package com.adam.zk.support;

import com.adam.zk.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class FoodIndicator implements HealthIndicator {
    @Autowired
    private FoodService foodService;

    @Override
    public Health health() {
        long count = foodService.getFoodCount();
        Health health;
        if (count > 0) {
            health = Health.up()
                    .withDetail("count", count)
                    .withDetail("message", "We have enough food.")
                    .build();
        } else {
            health = Health.down()
                    .withDetail("count", 0)
                    .withDetail("message", "We are out of food.")
                    .build();
        }
        return health;
    }
}
